<?php
	

defined('_JEXEC') or die('Restricted access');

$editor = &JFactory::getEditor();
$config = &JComponentHelper::getParams('com_nbeventmap'); 
JHtml::_('behavior.formvalidation');
JHtml::_('behavior.keepalive');


$_lat = $this->config->get('nbeventmap_centre_lat');
$_lng = $this->config->get('nbeventmap_centre_lng');
$_zoom = $this->config->get('nbeventmap_zoom');
if (empty($_lat)) {$_lat = 47.927385663;};
if (empty($_lng)) {$_lng = 2.1437072753;};
if (empty($_zoom)) {$_zoom = 10;};

?>
<link rel="stylesheet" href="components/com_nbeventmap/views/general.css" type="text/css" /> 
<script type="text/javascript" src="<?php echo JURI::root();?>media/netbase/js/jquery-1.5.1.min.js"></script>

<script type="text/javascript">
	Joomla.submitbutton = function(task) {
		if (task == 'cancel') {
			submitform(task);		
			return;
		}
		 else {
	            if (validateForm())
	                submitform( task );
	        }

		<?php
			
			
		?>
	}
</script>

<script language="javascript" type="text/javascript">
    var geocoder;
    var map;
    var marker1;

    function init() {
		UpdateAddress();
		geocoder = new google.maps.Geocoder();
        
		var lat, lng, zoom_carte;
        if(document.adminForm.glat.value!=0) lat = document.adminForm.glat.value;
        else lat = <?php echo $_lat?>;
        if(document.adminForm.glng.value!=0) lng = document.adminForm.glng.value;
        else lng = <?php echo $_lng?>;
        if(document.adminForm.gzoom.value!=0) zoom_carte = parseInt(document.adminForm.gzoom.value);
        else zoom_carte = <?php echo $_zoom?>;

		var latlng = new google.maps.LatLng(lat, lng);
		var myOptions = {
		  zoom: zoom_carte,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		map = new google.maps.Map(document.getElementById("map"), myOptions);

	  google.maps.event.addListener(map, "bounds_changed", function() {
		   document.adminForm.gzoom.value = map.getZoom();
	  });

      // Create a draggable marker which will later on be binded to a
      marker1 = new google.maps.Marker({
          map: map,
          position: new google.maps.LatLng(lat, lng),
          draggable: true,
          title: 'Drag me!'
      });
	  google.maps.event.addListener(marker1, "drag", function() {
		document.adminForm.glat.value = marker1.getPosition().lat();
		document.adminForm.glng.value = marker1.getPosition().lng();
	  });
    }

    // Register an event listener to fire when the page finishes loading.
    google.maps.event.addDomListener(window, 'load', init);
 
  
    function showAddress() {
		var address = document.adminForm.localisation.value;
		if (geocoder) {
			geocoder.geocode( { 'address' : address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
				  map.setCenter(results[0].geometry.location);
				  marker1.setPosition(results[0].geometry.location); 
					document.adminForm.glat.value = results[0].geometry.location.lat();
					document.adminForm.glng.value = results[0].geometry.location.lng();
				} else {
				  alert(address + " not found for the following reason: " + status);
				}
			})
		}
    }

    function getCoordinate() {
		var lat, lng;
        if(document.adminForm.glat.value!=0) lat = document.adminForm.glat.value;
        else lat = <?php echo $_lat?>;
        if(document.adminForm.glng.value!=0) lng = document.adminForm.glng.value;
        else lng = <?php echo $_lng?>;
        if(document.adminForm.gzoom.value!=0) zoom_carte = parseInt(document.adminForm.gzoom.value);
        else zoom = <?php echo $_zoom?>;

		var latlng = new google.maps.LatLng(lat, lng);
		map.setZoom(zoom_carte);
		map.setCenter(latlng);
		marker1.setPosition(latlng); 
    }
	
	function changeDisplayImage(chemin) {
		if (document.adminForm.img.value !='') {
			document.adminForm.imagelib.src=chemin + document.adminForm.img.value;
		} else {
			document.adminForm.imagelib.src=chemin+'blank.png';
		}
	}

	function changeDisplayIcon(chemin) {
		if (document.adminForm.icon.value !='') {
			document.adminForm.imageicon.src='templates/bluestork/images/header/' + document.adminForm.icon.value;
		} else {
			document.adminForm.imageicon.src='<?php echo JURI::root().$this->config->get('nbeventmap_chemin_img')?>'+'blank.png';
		}
	}

    function addphoto(file, indice){
        var optX = new Option(file, file);
        var selX = document.forms[0].elements['img'];
        var lenghX = selX.length;
        selX.options[lenghX] = optX;
                selX.options[lenghX].selected = true;
    }

	function jSelectArticle(id, title, object) {
		document.getElementById(object + '_id').value = id;
		document.getElementById(object + '_name').value = title;
		document.getElementById('sbox-window').close();
	}

	function UpdateAddress(){
 		//document.adminForm.localisation.value = document.adminForm.adresse.value + " " + document.adminForm.adresse2.value + " " + document.adminForm.codepostal.value + " " + document.adminForm.ville.value + " " + document.adminForm.departement.value + ", " + document.adminForm.pay.value;	
		document.adminForm.localisation.value = document.adminForm.ville.value + ", " + document.adminForm.pay.value;
	}

	function IsReal(id){
		MonNombre=document.getElementById(id).value;
		if(isNaN(MonNombre))
		{
			alert("\"" + MonNombre + "\" <?php echo JText::_( 'NBEVENTMAP_PAS_NOMBRE' ); ?>");
			return false;
		}	
		return true;
	}

	function jSelectArticle(id, title, catid, object) {
		document.getElementById('id_id').value = id;
		document.getElementById('id_name').value = title;
		SqueezeBox.close();
	}

	function newFile() {
		var file_tmp = '<br/><input type="file" name="files[]" size="40"/>';
		jQuery('#files').append(file_tmp);
	}
	function validateForm()
    {
		var form = document.adminForm;
		if (form.start_date.value == "")
        {
            alert("<?php echo JText::_( 'Please input start date!'); ?>");
            form.start_date.focus();
            return false;
        }
		if (form.end_date.value == "")
        {
            alert("<?php echo JText::_( 'Please input end date!'); ?>");
            form.end_date.focus();
            return false;
        }
		 return true;
	}
	
</script>

<form action="index.php" method="post" name="adminForm" id="item-form" class="form-validate nbeventmap" enctype='multipart/form-data'>
<div>
	<fieldset class="adminform">
	<legend><?php echo JText::_( 'NBEVENTMAP_DETAILS' ); ?></legend>
	<table class="admintable">
		<tr>
			<td width="110" class="key">
				<label for="title">
					<?php echo JText::_( 'NBEVENTMAP_NOM' ); ?>:<span class="star">&nbsp;*</span>
				</label>
			</td>
			<td>
				<input class="inputbox required" type="text" name="nom" id="nom" size="60" value="<?php echo str_replace('"', '&quot;',$this->nbeventmap->nom); ?>" />
			</td>
		</tr>
		<tr>
			<td width="100" class="key">
				<label for="alias">
					<?php echo JText::_( 'Alias' ); ?>:
				</label>
			</td>
			<td>
				<input class="text_area" type="text" name="alias" id="alias" size="32" maxlength="250" value="<?php echo $this->nbeventmap->alias;?>" />
			</td>
		</tr>
		<tr>
			<td width="110" class="key">
				<label>
					<?php echo JText::_( 'JCATEGORY' ); ?>:<span class="star">&nbsp;*</span>
                </label>
			</td>
			<td>
				<?php
					echo $this->lists['catid'];
				?>
			</td>
			</tr>
		
		 <tr>
            <td width="110" class="key">
                <label>
                    <?php echo JText::_('Start date'); ?>:
                </label>
            </td>
            <td>
                <?php echo JHTML::_('calendar', $this->nbeventmap->start_date, 'start_date', 'start_date', '%Y-%m-%d', array('class'=>'text_area', 'size'=>'32',  'maxlength'=>'19')); ?>
            </td>
        </tr>
         <tr>
            <td width="110" class="key">
                <label>
                    <?php echo JText::_('End date'); ?>:
                </label>
            </td>
            <td>
                <?php echo JHTML::_('calendar', $this->nbeventmap->end_date, 'end_date', 'end_date', '%Y-%m-%d', array('class'=>'text_area', 'size'=>'32',  'maxlength'=>'19')); ?>
            </td>
        </tr>
		
		<tr>
			<td class="key">
				<label for="lag">
					<?php echo JText::_( 'NBEVENTMAP_VILLE' ); ?>:
				</label>
			</td>
			<td>
				<input class="inputbox" type="text" name="ville" id="ville" onchange="UpdateAddress();" size="60" value="<?php echo str_replace('"', '&quot;',$this->nbeventmap->ville); ?>" />
			</td>
		</tr>
	
		<tr>
			<td class="key">
				<label for="lag">
					<?php echo JText::_( 'NBEVENTMAP_PAYS' ); ?>:
				</label>
			</td>
			<td>
				<input class="inputbox" type="text" name="pay" id="pay" onchange="UpdateAddress();" size="60" value="<?php echo str_replace('"', '&quot;',$this->nbeventmap->pay); ?>" />
			</td>
		</tr>
		
		
        <tr>
            <td width="110" class="key">
            	<label for="title">       
            		<?php echo JText::_('NBEVENTMAP_MAJ_ADRESSE'); ?>:
            	</label>
            </td>
            <td valign="top">
            	<input type="text" style="width:70%" name="localisation" value="" />
            	<!-- button find -->
            	<input type="button" onclick="showAddress();" value="<?php echo JText::_('NBEVENTMAP_CHERCHER'); ?>" />
            </td>
		</tr>
        <tr>
            <td width="110" class="key">
            	<label for="title">
                	<?php echo JText::_('NBEVENTMAP_LAT'); ?> - <?php echo JText::_('NBEVENTMAP_LON'); ?> - Zoom:
              	</label>
            </td>
            <td valign="top">
                <input class="inputbox validate-numeric" onblur="IsReal('glat');" type="text" name="glat" id="glat" size="20" value="<?php echo $this->nbeventmap->glat ?>" />
                <input class="inputbox validate-numeric" onblur="IsReal('glng');" type="text" name="glng" id="glng" size="20" value="<?php echo $this->nbeventmap->glng ?>" />
                <input class="inputbox validate-numeric" onblur="IsReal('gzoom');" type="text" name="gzoom" id="gzoom" size="2" value="<?php echo $this->nbeventmap->gzoom ?>" />
                <input type="button" onclick="getCoordinate();" value="<?php echo JText::_('NBEVENTMAP_CHERCHER_COORDONNEES'); ?>" />
            </td>
    	</tr>
        <tr>
            <td width="100" class="key">
              	<label for="title">
              		<?php echo JText::_('NBEVENTMAP_CARTE'); ?>:
              	</label>
            </td>
            <td>
            	<div id="map" style="width: 600px; height: 500px; overflow:hidden;"></div>
            </td>
        </tr>
       
    	<tr>
            <td width="110" class="key">
            	<label for="title">
            	<?php echo JText::_( 'NBEVENTMAP_MESSAGE' ); ?>:
            	</label>
            </td>
        	<td valign="top" class="inputbox">
            	<?php
				echo $editor->display( 'text_message', htmlspecialchars($this->nbeventmap->text, ENT_COMPAT, 'UTF-8'), '100%', '300', '75', '20', true, 'text_message');
				?>
        	</td>
		</tr>
		<tr>
            <td width="100" class="key">
              	<label for="title">
              		<?php echo JText::_('Images'); ?>:
              	</label>
            </td>
            <td>
            	<div id="files">
            	<input type="file" name="files[]" size="40"/>
            	</div>
            	<br/><a href="" onclick="newFile();return false;"><?php echo JText::_('Add new file.');?></a>
            </td>
        </tr>
        <!-- start -->
        
	<?php if(!empty($this->files)) { ?>				
	<tr><td colspan="2">
		<div id="editcell1">
			<table class="adminlist">
				<thead>
					<tr align="left">
						<th width="5">
							<?php echo JText::_( '#' ); ?>
						</th>
						<th align="left">
							<?php
								echo JText::_( 'File name' ); 
							?>
						</th>
						<th>
							<?php
								echo JText::_( 'Images' ); 
							?>
						</th>
						<th width="100">
							<?php
								echo JText::_( 'Delete file' ); 
							?>
						</th>
					</tr>
				</thead>
				<tbody>
		<?php
		$k = 0;
		for ($i=0,$n=count($this->files);$i<$n;$i++)
		{		
			$row =& $this->files[$i];
		?>
				<tr class="<?php echo "row$k"; ?>">
					<td><?php echo $row->id; ?></td>
					<td><?php echo $row->name; ?></td>
					<td><img src="<?php echo JURI::root();?>media/netbase/images/<?php echo $row->name; ?>" width="50" height="50"></td>
					<td align="center"><a href="index.php?option=com_nbeventmap&controller=nbeventmap&task=deleteFiles&cid=<?php echo $row->id; ?>"><img src="<?php echo JURI::root().'media/netbase/icon/delete.png'; ?>" title="Delete" alt="Delete file"></a></td>
				</tr>
		<?php	$k=1-$k;
		}
		?>
				</tbody>
					</table>
	</div>		
	</td></tr>
	<?php } ?>		
        <!-- end  -->
        <tr>
			<td width="100" class="key">
				<label for="alias">
					<?php echo JText::_( 'Website' ); ?>:
				</label>
			</td>
			<td>
				<input class="text_area" type="text" name="web" id="web" size="72" maxlength="250" value="<?php echo $this->nbeventmap->web;?>" />
			</td>
		</tr>
		 <tr>
			<td width="100" class="key">
				<label for="alias">
					<?php echo JText::_( 'Url Youtube Video' ); ?>:
				</label>
			</td>
			<td>
				<input class="text_area" type="text" name="url_video" id="url_video" size="72" maxlength="250" value="<?php echo $this->nbeventmap->url_video;?>" />
			</td>
		</tr>
		<tr>
			<td width="120" class="key">
				<?php echo JText::_( 'JPUBLISHED' ); ?>:
			</td>
			<td >
				<?php echo JHTML::_( 'select.booleanlist',  'published', 'class="inputbox"', $this->nbeventmap->published ); ?>
			</td>
		</tr>
	</table>
	</fieldset>
</div>
<div class="clr"></div>
<div class="clr"></div>

<input type="hidden" name="userid" value="<?php echo $this->nbeventmap->userid; ?>" />
<input type="hidden" name="option" value="com_nbeventmap" />
<input type="hidden" name="id" value="<?php echo $this->nbeventmap->id; ?>" />
<input type="hidden" name="task" value="deleteFiles" />
<input type="hidden" name="controller" value="nbeventmap" />
</form>
<div class="copyright" align="center">
	<br />
	<?php ?>
</div>

